<%@ page contentType="text/html; charset=utf-8" language="java" extends="org.zizi.service.JspPageServlet" %>
<%@page import="java.util.Date"%>
<%@page import="org.zizi.util.DateUtil"%>
<%@page import="org.zizi.modal.VatCredit"%>
<%@page import="java.util.List"%>
<%@page import="org.zizi.util.PagedList"%>
<%@page import="org.zizi.util.Pagging"%>
<%@page import="org.zizi.manager.impl.DaoManager"%>
<%@page import="org.zizi.manager.Managers"%>
<%@page import="org.zizi.dao.VatCreditDao"%>
<%@page import="org.zizi.modal.User"%>
<%@page import="org.zizi.manager.LoginManager"%>
<%@page import="org.zizi.util.RequestParamReplace"%>
<%@page import="org.zizi.dao.InlandRevenueOrgDao"%>
<%@page import="org.zizi.dao.UserInfoDao"%>
<%@page import="org.zizi.modal.UserInfo"%>
<%@page import="org.zizi.modal.VatCreditInvoice"%>

<%
	User u = null;
	if (session != null) u = Managers.get(LoginManager.class).getUser(session.getId());
	if (u == null) response.sendRedirect("/login.jsp");

	Date now = new Date();
	int year = DateUtil.getYear(now);
	int month = DateUtil.getMonth(now);
	
	String szBeginYear = request.getParameter("begin_year");
	String szBeginMonth = request.getParameter("begin_month");
	String szEndYear = request.getParameter("end_year");
	String szEndMonth = request.getParameter("end_month");
	
	int beginYear = szBeginYear == null ? year : Integer.parseInt(szBeginYear);
	int beginMonth = szBeginMonth == null ? 1 : Integer.parseInt(szBeginMonth);
	int endYear = szEndYear == null ? year : Integer.parseInt(szEndYear);
	int endMonth = szEndMonth == null ? month : Integer.parseInt(szEndMonth);
	
	String identify = request.getParameter("identify");
	if (identify == null) identify = "";
	
	String szPage = request.getParameter("page");
	int p = szPage == null ? 1 : Integer.parseInt(szPage);
	
	Pagging pagging = new Pagging(p);
	
	//do query
	VatCreditDao dao = Managers.get(DaoManager.class).getDao(VatCreditDao.class);
	PagedList<VatCredit> list = null;
	if (!identify.isEmpty() && u != null)
	{
		//权限
		InlandRevenueOrgDao orgDao = Managers.get(DaoManager.class).getDao(InlandRevenueOrgDao.class);
		List<String> codes = orgDao.getManagingInlandRevenueOrgCode(u.getOrgCode());
		
		//
		UserInfoDao uiDao = Managers.get(DaoManager.class).getDao(UserInfoDao.class);
		UserInfo ui = uiDao.getUserInfo(identify);
		
		if (ui != null && codes.contains(ui.getOrgCode()))
		{
			list = dao.listVatCredits(identify, beginYear, beginMonth, endYear, endMonth, pagging.getOffset(), pagging.getLimit());
			if (list != null) pagging.setTotal(list.getTotal());
		}
	}
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>专用设置和技术维护费抵减系统</title>
<jsp:include page="engine.jsp" ></jsp:include>
<script type="text/javascript">

function view(id)
{
	window.showModalDialog("view.jsp?id=" + id, null, "dialogWidth=825px; dialogHeight=625px;");
}

function edit(id)
{
	window.showModalDialog("edit.jsp?id=" + id, null, "dialogWidth=1080px; dialogHeight=625px;");
	location.reload();
}

function invalid(id)
{
	if (!window.confirm("确定作废吗？作废后若要重新生效，需要重新编辑并提交！")) return;
	
	var action = new Action("InvalidVatCredit");
	action.id = id;
	
	var callback = function(result)
	{
		location.reload();
	}
	Rpc.call(action, callback);
}

function deletei(id)
{
	if (!confirm("删除后，数据不可恢复。确定删除吗？")) return;
	
	var action = new Action("DeleteVatCredit");
	action.id = id;
	
	var callback = function(result)
	{
		location.reload();
	}
	Rpc.call(action, callback);
}

function jump(url)
{
	location.href = url;
}

function logout()
{
	var action = new Action("Logout");
	var callback = function(result)
	{
		location.href = "/login.jsp";
	}
	Rpc.call(action, callback);
}

</script>
<link href="css/common.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.history_table {font-size:12px; border-collapse:collapse; width:890px; margin-top:5px;}
.history_table th {border:1px solid #CCC;}
.history_table td {border:1px solid #CCC;}
.history_table a {text-decoration:none; color:#000;}
.history_table a:hover {color:#F00;}
.pagging {display:inline; margin-right:55px; font-size:12px; float:right; margin-top:10px;}
.pagging a {color:#333; width:20px; height:20px; border:1px solid #CCC; margin:3px; float:left; text-align:center; line-height:20px; text-decoration:none; background-color:#FFF;}
.pagging a:hover {color:#F00; background-color:#CCC;}
.pagging a.current {color:#F00; background-color:#CCC;}
.search_bar_table {font-size:12px; margin:10px 0 10px 0;}
.search_bar_table td {border:0;}
.search_bar_table .input_width120 {width:120px;}
.search_bar_table span {padding-left:10px;}
</style>
</head>
<body onload="resize()">
	<div id="container">
		<div class="top"></div>
		<div class="nav">
			<div class="navbuttons">
				<div class="navitem" style="background:url(images/main/submit_n.gif)" onclick="jump('/submit.jsp')"></div>
				<div class="navitem" style="background:url(images/main/history_a.gif)" onclick="jump('/history.jsp')"></div>
				<div class="navitem" style="background:url(images/main/types_n.gif)" onclick="jump('/types.jsp')"></div>
				<div class="navitem" style="background:url(images/main/empower_n.gif)" onclick="jump('/empower.jsp')"></div>
			</div>
			<div class="navcmds">
				<div style="background:url(images/main/logout.gif) no-repeat;"><a href="#" onclick="logout(); return false;">退出系统</a></div>
			</div>
		</div>
		<div class="content">
			<table class="history_table" cellspacing="0" cellpadding="0" border="0" align="center">
				<tr>
					<td colspan="8" style="border:0;">
						<form method="get">
						<table class="search_bar_table" cellspacing="0" cellpadding="0" border="0" align="center">
							<tr>
								<td><span>纳税人识别号：</span></td>
								<td><input type="text" class="input_width120" name="identify" value="<%=identify %>" /></td>
								<td><span>起始时间：</span></td>
								<td>
									<select name="begin_year">
										<%
											for (int i = 0; i < 10; i++)
											{
												int y = year + i - 4;
												
												if (y == beginYear)
													out.println(String.format("<option value=%d  selected>%d 年</option>", y, y));
												else
													out.println(String.format("<option value=%d>%d 年</option>", y, y));
											}
										%>
									</select>
								</td>
								<td>
									<select name="begin_month">
										<% 
											for (int i = 0; i < 12; i++)
											{
												int m = i + 1;
												if (m == beginMonth)
													out.println(String.format("<option value=%d  selected>%d 月</option>", m, m));
												else
													out.println(String.format("<option value=%d>%d 月</option>", m, m));
											}
										%>
									</select>
								</td>
								<td><span>结束时间：</span></td>
								<td>
									<select name="end_year">
										<%
											for (int i = 0; i < 10; i++)
											{
												int y = year + i - 4;
												
												if (y == endYear)
													out.println(String.format("<option value=%d  selected>%d 年</option>", y, y));
												else
													out.println(String.format("<option value=%d>%d 年</option>", y, y));
											}
										%>
									</select>
								</td>
								<td>
									<select name="end_month">
										<% 
											for (int i = 0; i < 12; i++)
											{
												int m = i + 1;
												
												if (m == endMonth)
													out.println(String.format("<option value=%d  selected>%d 月</option>", m, m));
												else
													out.println(String.format("<option value=%d>%d 月</option>", m, m));
											}
										%>
									</select>
								</td>
								<td><input type="submit" value="查询"/></td>
							</tr>
						</table>
						</form>
					</td>
				</tr>
				<tr>
					<th>填开日期</th>
					<th>纳税人识别号</th>
					<th>本期可抵减税额</th>
					<th>本期申报抵减税额</th>
					<th>本期未抵减税额</th>
					<th>所属期</th>
					<th>状态</th>
					<th>操作</th>
				</tr>
				<%
					if (list != null)
					{
						for (VatCredit c : list)
						{
							out.println("<tr>");
							out.println("<td>" + DateUtil.format(c.getSubmitTime()) + "</td>");
							out.println("<td>" + c.getIdentify() + "</td>");
							out.println("<td>" + (c.getApplyVatCredit() + c.getThisLeftCredit()) + " 元</td>");
							out.println("<td>" + c.getApplyVatCredit() + " 元</td>");
							out.println("<td>" + c.getThisLeftCredit() + " 元</td>");
							out.println("<td>" + c.getYear() + "-" + c.getMonth() + "</td>");
							out.println("<td>" + (c.isValid() ? "-" : "已作废") + "</td>");
							
							String operation = "<a href=\"#\" onclick=\"view(" + c.getId() + "); return false;\">[查看]</a>";
							
							if (u.isMaintainCredit())
							{
								if (c.isValid())
								{
									operation += "<a href=\"#\" onclick=\"edit(" + c.getId() + "); return false;\">[编辑]</a>";
									operation += "<a href=\"#\" onclick=\"invalid(" + c.getId() + "); return false;\">[作废]</a>";
								}
								else
								{
									operation += "<a href=\"#\" onclick=\"edit(" + c.getId() + "); return false;\">[生效]</a>";
								}
								operation += "<a href=\"#\" onclick=\"deletei(" + c.getId() + "); return false;\">[删除]</a>";
							}
							
							out.println("<td>" + operation + "</td>");
							out.println("</tr>");
						}
					}
				%>
			</table>
			<div class="pagging">
				<%
					String query = request.getQueryString();
					query = RequestParamReplace.replace(query, "page", "@PAGE");
					
					String url = request.getRequestURL().toString();
					url += query;
					
					String source = "<a href=\"" + url + "\">@TEXT</a>";
					String current = "<a href=\"" + url + "\" class=\"current\">@TEXT</a>";
					
					pagging.draw(10, out, source, current);
				%>
			</div>
		</div>
	</div>
</body>
</html>